<?php
/**
 * Performs validations during add/modify user
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @category VaselinEngine
 * @package Usermanagement Module
 * @subpackage Helper
 * @license  New BSD License
 *
 */
class Helpers_Usermanagement_Validate{
	
	/**
	 * Table name with users
	 *
	 * @var string
	 */
	private $_table_name;
	
	/**
	 * Initialization
	 *
	 */
	public function __construct(){
		$this->_table_name = TABLE_PREFIX.'users';
	}
	
	/**
	 * Validates passwords match
	 *
	 * @param string $password
	 * @param string $password_confirm
	 * @return bool - true if match
	 */
	public function ifPasswordMatch($password, $password_confirm) {
		if ($password != $password_confirm){
			return false;
		}else{	
			return true;
		}	
	}
		
	/**
	 * Validates if username is available
	 *
	 * @param string $username
	 * @param int $user_id
	 * @return bool - true if available
	 */
	public function ifUsernameAvailable($username, $user_id = NULL) {
		$exclude = NULL;
		if($user_id){
			$exclude = array('field'=>'user_id', 'value'=>$user_id);
		}
		$_v = new Zend_Validate_Db_RecordExists($this->_table_name,'user_login',$exclude);
		
		if(!$_v->isValid($username)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * Validates if email available
	 *
	 * @param string $email
	 * @param int $user_id
	 * @return bool - true if available
	 */
	public function ifEmailAvailable($email, $user_id = NULL) {
		$exclude = NULL;
		if($user_id){
			$exclude = array('field'=>'user_id', 'value'=>$user_id);
		}
		$_v = new Zend_Validate_Db_RecordExists($this->_table_name,'user_email',$exclude);
		
		if(!$_v->isValid($email)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * Validates catcha field
	 *
	 * @param unknown_type $request
	 * @return bool
	 */
	public function validateCaptcha($request) {
		
		$captcha = new Zend_Captcha_Image(array(	
   			 'name' => 'captcha',		
    		 'wordLen' => 6,
    		 'timeout' => 300,
		));
		
		if($captcha->isValid($request['captcha']['input'],$request)){
			return true;
		}else{
			return false;
		}
	}
}

